<template>
  <view>
    <view class="login-container">
      <uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
      <button open-type="getUserInfo" @click="getUserInfo" type="primary" class="btn-login">一键登录</button>
      <!-- 登录提示 -->
      <view class="tips-text">
        登录后尽享更多权益
      </view>
    </view>
  </view>
</template>

<script>
  import { mapMutations,mapState } from 'vuex'
  export default {
    name:"my-login",
    data() {
      return {
        
      };
    },
    computed: {
      ...mapState('m_user',['redirect'])
    },
    methods: {
      // 获取用户基本信息
     async getUserInfo() {
      const [error,result] =  await uni.getUserProfile({
          desc:'用于完成登录',
        }).catch(err => err)
        // console.log(error)
        // console.log(result)
        if(error && error.errMsg === 'getUserProfile:fail auth deny') {
          return uni.$showMsg('您取消了登录授权!')
        }
        // console.log(e.detail.userInfo)
        this.updateUserInfo(result.userInfo)
        // console.log(result.userInfo)
        this.getToken(result)
      },
      ...mapMutations('m_user',['updateUserInfo','updateReairect']),
      ...mapMutations('m_user',['updateToken']),
      // 获取token信息
     async getToken(info) {
       const [err,res] = await uni.login().catch(err => err)
       // 判断是否登陆成功
       if(err || res.errMsg !== 'login:ok') return uni.$showMsg('登录失败')
       // console.log(res.code)
       // console.log(info)
       // 准备登陆对象
       const query = {
         code:res.code,
         encryptedData: info.encryptedData,
         iv: info.iv, 
         rawData: info.rawData,
         signature: info.signature,
       }
       // console.log(query)
       // 获取token
      const {data:loginResult} = await uni.$http.post('users/wxlogin',query)
      console.log(loginResult)
      let token ="Bearer 607d5e3fd304487aa4f500789991b94f"
     this.updateToken(token)
      uni.$showMsg('登录成功')
      this.navgiterBy()
      },
      navgiterBy() {
        if(this.redirect && this.redirect.openType ==='switchTab') {
          uni.switchTab({
            url:this.redirect.from,
            complete: ()=> {
              this.updateReairect(null)
            }
          })
        }
      }
    }
  }
</script>

<style lang="scss">
.login-container {
  height: 750rpx;
  background-color: #F8F8F8;
  display: flex;
  align-items: center;
  flex-direction: column;
  justify-content: center;
  overflow: hidden;
  position: relative;
  &::after {
    content: ' ';
    display: block;
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 40px;
    border-radius: 100%;
    transform: translateY(50%);
    background-color: #fff;
  }
  .btn-login {
    width: 90%;
    border-radius: 100px;
    margin:15px 0; 
    background-color: #C00000;
  }
  .tips-text {
    font-size: 12px;
    color: gray;
  }
}
</style>